package com.fone.player.service;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

import com.fone.player.util.L;

/**
 * 通知轮询服务(service)
 * 
 * @author JasonZue
 * @since 2014-5-20
 */

public class NotificationService extends Service {

	private static final String TAG = "NotificationService";

	private IntentFilter notificationFilter;
	private BroadcastReceiver notificationReceiver;
	private NotificationHandler notificationHandler;
	/** 节目单预约的处理线程 */
	private LiveCardHandler liveCardHandler;

	private boolean isServiceStart = false;
	public final static String NOTICICATION_ACTION = "notification_attention_action";
	/** 节目单广播 */
	private final static String LIVECARD_ACTION = "notification_livecard_action";

//	private Notification notification;
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;

	}

	@Override
	public void onCreate() {
		L.v(TAG, "onCreate", "start");
		super.onCreate();

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
//		notification = new Notification();
//		startForeground(1, notification);
		
		if (!isServiceStart) {
			L.v(TAG, "NotificationService-->onStartCommand");
			registerNotificationBrocast();
			isServiceStart = true;
			if (notificationHandler == null) {
				notificationHandler = new NotificationHandler(
						getApplicationContext());
			}
			notificationHandler.startNotification();

			if (liveCardHandler == null) {
				liveCardHandler = new LiveCardHandler(this);
			}
			liveCardHandler.start();
		}

		return START_NOT_STICKY;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
//		stopForeground(true);
		// 设置服务启动标志 关闭
		isServiceStart = false;
		// 注销通知广播
		unRegisterNotificationBrocast();
		if (notificationHandler != null) {
			notificationHandler.stopNotification();
			notificationHandler = null;
		}

		if (liveCardHandler != null) {
			liveCardHandler.interrupt();
			liveCardHandler = null;
		}
	}

	/**
	 * 注册广播接收关注信息
	 * 
	 * @return void
	 * @throws
	 */
	public void registerNotificationBrocast() {
		L.v(TAG, "registerNotificationBrocast-->>register");
		if (notificationFilter == null) {
			notificationFilter = new IntentFilter();
			notificationFilter.addAction(NOTICICATION_ACTION);
			notificationFilter.addAction(LIVECARD_ACTION);
		}
		if (notificationReceiver == null) {
			notificationReceiver = new BroadcastReceiver() {

				@Override
				public void onReceive(Context context, Intent intent) {
					// TODO 修改接受信息
					// ArrayList<String> attTimes= (ArrayList<String>)
					// intent.getSerializableExtra("attTime");
					// String anchorId = intent.getStringExtra("anchorId");
					// L.v(TAG, "onReceive>>anchorId:" + anchorId +" "+
					// attTimes.size());
					// for(int i = 0 ; i < attTimes.size() ; i++){
					// L.v(TAG, "onReceive>>anchorIDs:" + attTimes.get(i));
					// Anchor anchor = new Anchor();
					// anchor.anchorId = anchorId;
					// anchor.time = attTimes.get(i);
					// notificationDB.insertNotificationInfo(anchor);
					// }
				}
			};
		}
		registerReceiver(notificationReceiver, notificationFilter);
	}

	/**
	 * 解除接收关注信息广播
	 */
	public void unRegisterNotificationBrocast() {
		L.v(TAG, "unRegisterNotificationBrocast>>unRegister");
		if (notificationReceiver != null) {
			unregisterReceiver(notificationReceiver);
			notificationReceiver = null;
		}
	}
}
